package utils

import (
	"WchimeGinSystem/utils/errmsg"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Success int         `json:"success"`
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
}

func NewResponse() *Response {
	return &Response{
		Success: 1,
		Code:    1,
		Msg:     "",
		Data:    nil,
	}
}

type Wrapper struct {
	ctx *gin.Context
}

func RespContext(ctx *gin.Context) *Wrapper {
	return &Wrapper{ctx: ctx}
}

func (wrap Wrapper) SuccessResp(data interface{}) {
	resp := NewResponse()

	msg := "success"

	resp.Code = 1
	resp.Success = 1
	resp.Msg = msg
	resp.Data = data
	wrap.ctx.JSON(200, resp)
}

func (wrap Wrapper) ErrorResp(data interface{}, code int, msg string) {
	resp := NewResponse()
	if msg == "" {
		msg = errmsg.GetErrMsg(code)
	}
	resp.Code = code
	resp.Success = 0
	resp.Msg = msg
	resp.Data = data
	wrap.ctx.JSON(200, resp)
}
